<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Verbs</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="classics.css" rel="stylesheet" type="text/css">
</head>
<?php

try {
	$db = new PDO('sqlite:LatinVerbs.db3');
	$q = $db->query('SELECT DISTINCT type FROM verbs;');
	$tmp = $q->fetchAll();
	$type = $tmp[array_rand($tmp)]['type'];
//	echo "<H1>$type</H1>\n";
	$q = $db->query("SELECT DISTINCT verb, meaning FROM verbs WHERE type='$type';");
	$tmp = $q->fetchAll();
	$vb = $tmp[array_rand($tmp)];
	$verb = $vb['verb'];
	$meaning = $vb['meaning'];
	$persons = array('I', 'thou', 'he', 'we', 'ye', 'they', 'Other');
	$person = $persons[array_rand($persons)];
	
	if ($person != 'Other') {
		$vb = array();
		$q = $db->query("SELECT voice, mood, tense, $person FROM verbs WHERE verb='$verb' AND $person NOT NULL;");
		
		$tmp = "";
		foreach( $q->fetchAll() as $row) {
			$vb[$row['mood']][$row['voice']][$row['tense']] = $row[$person];
			$tenses[$row['tense']] = True;
		}
		$vtab = "<caption><b>".$verb."</b>&nbsp;&nbsp;<i>$meaning</i> - ".$person."</caption>\n";
		
		
		$mds = "";
		$vcs = "";
		$n = 1;
		foreach(array('indicative', 'subjunctive', 'imperative') as $mood) {
			if (isset($vb[$mood])) {
				$mds = $mds."<th colspan=".count($vb[$mood])." align=\"center\">$mood</th>";
				foreach(array('active', 'passive') as $voice) {
					if (isset($vb[$mood][$voice])) {
						$vcs = $vcs."<th align=\"center\">".$voice."</th>";
						$n++;
					}
				}
			}
		}
		$b = floor($n/2);
		$a = $n - $b;
		$vtab = $vtab."<tr class=\"separate-above\"><td></td>$mds</tr>\n";
		$vtab = $vtab."<tr><td></td>$vcs</tr>\n";
		$alltenses = array('present', 'imperfect', 'future', 'perfect', 'pluperfect', 'future perfect');
		shuffle($alltenses);
		foreach($alltenses as $tense) {
			if (! isset($tenses[$tense]) ) {
				continue;
			}
			$vtab = $vtab . "<tr><th>$tense</th>";
			foreach(array('indicative', 'subjunctive', 'imperative') as $mood) {
				if (! isset($vb[$mood])) {
					continue;
				}
				foreach(array('active', 'passive') as $voice) {
					if (! isset($vb[$mood][$voice])) {
						continue;
					}
					$vtab = $vtab . "<td align=\"center\">";
					if (isset($vb[$mood][$voice][$tense])) {
						$tmp = explode('|', $vb[$mood][$voice][$tense]);
						foreach($tmp as $wd) {
							$txts[] = $wd;
							$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\" size=\"15\">";
						}
					}
					$vtab = $vtab . "</td>";
				}
			}
			$vtab = $vtab . "</tr>\n";
		}
	} else {
		$n = 1;
		$vtab = "<caption><b>".$verb."</b>&nbsp;&nbsp;<i>$meaning</i></caption>\n";
		$vb = array();
		$q = $db->query("SELECT * FROM infinitive WHERE verb='$verb';");
		
		$voices = array();
		foreach( $q->fetchAll() as $row) {
			$vb['infinitive'][$row['voice']] = $row;
			$voices[$row['voice']] = True;
		}
		$q = $db->query("SELECT * FROM participle WHERE verb='$verb';");
		foreach( $q->fetchAll() as $row) {
			$vb['participle'] = $row;
			if (mb_strlen($row['perfect'], 'UTF8') > 0 || mb_strlen($row['gerundive'], 'UTF8') > 0) {
				$voices['passive'] = True;
			}
			if (mb_strlen($row['present'], 'UTF8') > 0 || mb_strlen($row['future'], 'UTF8') > 0) {
				$voices['active'] = True;
			}
		}
		
		$cases = array();
		$q = $db->query("SELECT * FROM gerund WHERE verb='$verb';");
		foreach( $q->fetchAll() as $row) {
			$vb['gerund'] = $row;
			foreach(array('genitive', 'dative', 'accusative', 'ablative') as $case) {
				if (mb_strlen($row[$case], 'UTF8') > 0) {
					$cases[$case] = true;
				}
			}
		}
		
		$q = $db->query("SELECT * FROM supine WHERE verb='$verb';");
		foreach( $q->fetchAll() as $row) {
			$vb['supine'] = $row;
			foreach(array('accusative', 'ablative') as $case) {
				if (mb_strlen($row[$case], 'UTF8') > 0) {
					$cases[$case] = true;
				}
			}
		}
		
		$vcs = "";
		foreach(array('active', 'passive') as $voice) {
			if (isset($voices[$voice])) {
				$vcs .= "<th align=\"center\">$voice</th>";
			}
		}
		$n = count($voices) + 1;
		$b = floor($n/2);
		$a = $n - $b;
		
		$vtab .= "<tr bgcolor=\"CadetBlue\" class=\"separate-above\"><th>Infinitive</th>$vcs</tr>\n";
		$tenses = array('present', 'future', 'perfect', 'gerundive');
		foreach($tenses as $tense) {
			if (! (isset($vb['infinitive']['active'][$tense]) || isset($vb['infinitive']['passive'][$tense]))) {
				continue;
			}
			
			$vtab .= "<tr><th>$tense</th>";
			foreach(array('active', 'passive') as $voice) {
				$vtab = $vtab."<td align=\"center\">";
				if (isset($vb['infinitive'][$voice][$tense])) {
					$tmp = explode('|', $vb['infinitive'][$voice][$tense]);
					foreach($tmp as $wd) {
						$txts[] = $wd;
						$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\" size=\"15\">";
					}
				}
				$vtab = $vtab . "</td>";
			}
			$vtab = $vtab."</tr>\n";
		}
		
		$vtab .= "<tr bgcolor=\"CadetBlue\"><th>Participle</th>$vcs</tr>\n";

		foreach($tenses as $tense) {
			if (! isset($vb['participle'][$tense])) {
				continue;
			}
			if ($tense == 'gerundive') {
				$vtab .= "<tr bgcolor=\"CadetBlue\"><td colspan=$n></td></tr>\n";
			}
			
			
			$vtab .= "<tr><th>$tense</th>";
			if ($tense == 'perfect' || $tense == 'gerundive') {
				$vtab .= "<td></td>";
			}

			$vtab = $vtab."<td align=\"center\">";
			if (isset($vb['participle'][$tense])) {
				$tmp = explode('|', $vb['participle'][$tense]);
				foreach($tmp as $wd) {
					$txts[] = $wd;
					$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\" size=\"15\">";
				}
			}
			$vtab .= "</td>";
			if ($tense == 'present' || $tense == 'future') {
				$vtab .= "<td></td>";
			}
			$vtab = $vtab."</tr>\n";
		}
		if (count($cases) > 0) {
			$vtab .= "<tr bgcolor=\"CadetBlue\"><td></td>";
			$types = array('gerund', 'supine');
			foreach($types as $type) {
				if (isset($vb[$type])) {
					$vtab .= "<th aligh=\"center\">$type</th>";
				}
			}
			$vtab .= "</tr>\n";
			foreach(array('genitive', 'dative', 'accusative', 'ablative') as $case) {
				if (! isset($cases[$case])) {
					continue;
				}
				$vtab .= "<tr><th>$case</th>";
				foreach($types as $type) {
					$vtab .= "<td align=\"center\">";
					if (isset($vb[$type][$case])) {
						$tmp = explode('|', $vb[$type][$case]);
						foreach($tmp as $wd) {
							$txts[] = $wd;
							$vtab = $vtab . "<input name=\"text".(count($txts) - 1)."\" type=\"text\" size=\"15\">";
						}
					}
					$vtab .= "</td>\n";
				}
				$vtab .= "</tr>\n";
			}
			
		}
		


	}
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

<script type="text/javascript">
function check() 
{

<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "if (document.n01.text".$i.".value=='".$txts[$i]."')\n{\n";
		print "\tdocument.n01.text".$i.".disabled=true;\n}\n";
	}
?>

}

function newWord() 
{
<?php
	for($i = 0, $nall = count($txts); $i < $nall; $i++) {
		print "document.n01.text".$i.".disabled=false;\n";
 		print "document.n01.text".$i.".value='';\n";
	}
?>
	location.reload();
}
</script>

<body>

<div id="container">
<div id="center">
<form name="n01" action="">
<table align="center">
<?php
	print $vtab;
?>
<tr class="separate-above">
	<td align="center" colspan=<?php echo $a; ?>><input name="Check" onclick="check()" value="Check" type="button"></td>
	<td align="center" colspan=<?php echo $b; ?>><input name="New Word" onclick="newWord()" value="New Word" type="button"></td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
